//
//  SelectDateViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class SelectDateViewController: KMViewController {
    
    var currentDate = "" //当前日期
    var dateBlock : ((String)->Void)? //选择日期回调

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubView()

    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        let selectDateView = SelectDateView()
        selectDateView.cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        selectDateView.confirmBtn.addTarget(self, action: #selector(selectDateAction), for: .touchUpInside)
        selectDateView.datePicker.addTarget(self, action: #selector(currentDate(datePicker:)), for: .valueChanged)
        self.view.addSubview(selectDateView)
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY.MM.dd"
        self.currentDate = dateFormatter.string(from: selectDateView.datePicker.date)
        
        selectDateView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    //MARK: 取消选择日期
    @objc fileprivate func cancelAction() {
        
        self.dismiss(animated: false, completion: nil)
    }
    
    //MARK: 选择日期
    @objc fileprivate func selectDateAction() {
        
        self.dateBlock!(currentDate)
        self.dismiss(animated: false, completion: nil)
    }
    
    //MARK: 当前日期
    @objc fileprivate func currentDate(datePicker:UIDatePicker) {
        
        let theDate = datePicker.date
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY.MM.dd"
        self.currentDate = dateFormatter.string(from: theDate)
    }
    
    //MARK: 点击屏幕隐藏日期选择框
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.dismiss(animated: false, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        
    }

}
